package com.edu.backend.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.edu.backend.entity.ExamLabel;
import com.edu.backend.entity.PagerInfo;
import com.edu.backend.service.ExamLabelService;
import com.edu.backend.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
public class ExamLabelController {

    @Autowired
    ExamLabelService examLabelService;
    @GetMapping("/examlabel")
    public R getLabelList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize, ExamLabel examLabel){
        PagerInfo<ExamLabel> page=new PagerInfo<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page=examLabelService.getExamLabelList(page,examLabel);
        String jsonStr = JSON.toJSONString(page, SerializerFeature.WriteDateUseDateFormat);
        Map<String, Object> map = JSON.parseObject(jsonStr, new TypeReference<Map<String, Object>>() {});
        return R.ok().data(map);
    }

    @DeleteMapping("/deleteLabel/{id}")
    public R deleteLabel(@PathVariable(value = "id") String id){
        Boolean result=examLabelService.deleteLabel(id);
        if(result){
            return R.ok();
        }else{
            return R.error();
        }
    }
    @DeleteMapping("/deleteLabel")
    public R deleteBatchLabel(@RequestParam(value = "ids") String ids){

        String str[]=ids.split(",");
        List<String> list= Arrays.asList(str);
        Boolean result=examLabelService.deleteBatch(list);
        if(result){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @PostMapping("/addLabel")
    public R addLabel(@RequestBody ExamLabel examLabel){
        Boolean result=examLabelService.addLabel(examLabel);
        if(result){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @PutMapping("/updateLabel")
    public R updateLabel(@RequestBody ExamLabel examLabel){
        System.out.println(examLabel.toString());
        Boolean result=examLabelService.updateLabel(examLabel);
        if(result){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @GetMapping("/getLabel/{id}")
    public R getLabel(@PathVariable(value = "id") String id){
        ExamLabel examLabel=examLabelService.getLabel(Integer.valueOf(id));
        String jsonStr = JSON.toJSONString(examLabel, SerializerFeature.WriteDateUseDateFormat);
        Map<String, Object> map = JSON.parseObject(jsonStr, new TypeReference<Map<String, Object>>() {});
        return R.ok().data(map);
    }
}
